package com.rabbit.videoplayer.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.blankj.utilcode.util.NetworkUtils
import com.rabbit.videoplayer.listener.OnVideoStatusListener
import com.rabbit.videoplayer.R
import com.rabbit.videoplayer.listener.OnErrorStatusListener

class ErrorStatusView(context: Context, attrs: AttributeSet?=null): ConstraintLayout(context, attrs), OnVideoStatusListener {

    private val btnRetry: View
    private val btnReload: View
    private val layoutConfirm: View
    private val tvErrorMsg: TextView
    private val tvConfirm: TextView
    private val tvCancel: TextView
    private var isAllowMobilePlay = false
    private var listener: OnErrorStatusListener? = null

    init {
        LayoutInflater.from(context).inflate(R.layout.view_wb_mix_error_status, this, true)
        tvErrorMsg = findViewById(R.id.tv_vod_error)
        btnRetry = findViewById(R.id.layout_retry)
        btnReload = findViewById(R.id.btn_reload)
        layoutConfirm = findViewById(R.id.layout_confirm)
        tvCancel = findViewById(R.id.tv_cancel)
        tvConfirm = findViewById(R.id.tv_confirm)

        btnRetry.setOnClickListener {
            listener?.onRetry()
            hide()
        }

        tvConfirm.setOnClickListener {
            if (errorType == 2) {
                isAllowMobilePlay = true
            }
            listener?.onRetry()
            hide()
        }

        tvCancel.setOnClickListener {
            listener?.onCancel()
            hide()
        }

        btnReload.setOnClickListener {
            listener?.onRetry()
        }

        hide()
    }


    fun hide(){
        this.visibility = View.GONE
        tvErrorMsg.text = ""
        layoutConfirm.visibility = GONE
        btnRetry.visibility = GONE
    }

    fun isShowing(): Boolean {
        return visibility != View.GONE
    }

    fun setErrorStatusListener(listener: OnErrorStatusListener){
        this.listener = listener
    }

    private fun showWarnStatus(msg: String) {
        this.visibility = View.VISIBLE
        layoutConfirm.visibility = GONE
        tvErrorMsg.text = msg
        tvErrorMsg.visibility = View.VISIBLE
        btnReload.visibility = View.GONE
        btnRetry.visibility = VISIBLE

    }

    fun showErrorStatus(msg: String){
        this.visibility = View.VISIBLE
        btnRetry.visibility = View.GONE
        tvErrorMsg.text = msg
        btnReload.visibility = View.GONE

        layoutConfirm.visibility = View.VISIBLE
    }

    private fun showCompleteStatus(){
        tvErrorMsg.text = ""
        btnRetry.visibility = GONE
        this.visibility = View.VISIBLE
        layoutConfirm.visibility = GONE
        btnReload.visibility = View.VISIBLE
    }

    override fun onVodInit(vararg init: Any?): Boolean {
        if (isErrorOrWarn()) { // 无网络
            return true
        }
        hide()
        return false
    }

    private var errorType = -1
    fun isErrorOrWarn(): Boolean {
        if (!NetworkUtils.isConnected()) { // 无网络
            errorType = 0
            showWarnStatus("无网络链接，请检查您设置是否正常链接到网络！")
            return true
        }
        // 判断是否使用代理播放
        if (NetworkUtils.isBehindProxy()) {
            errorType = 1
            showWarnStatus("请关闭代理后再次尝试播放！")
            return true
        }

        if (!isAllowMobilePlay && NetworkUtils.isMobileData()) { // 使用移动网络
            errorType = 2
            showErrorStatus( "您正在使用移动流量进行播放，请确认是否继续播放！")
            return true
        }
        return false
    }

    override fun onVodPrepared(vararg prepared: Any) {
        hide()
    }

    override fun onVodStart() {
        hide()
    }

    override fun onVodFirstFrameStart() {
        hide()
    }

    override fun onVodSwitchDisplayControlView() {

    }

    override fun onVodPause() {

    }

    override fun onVodStop() {
        hide()
    }

    override fun onVodError(vararg params: Any) {
        if (params.isEmpty()) return
        // 判断4g和网络
        if (!NetworkUtils.isConnected()) { // 无网络
            showWarnStatus("无网络链接，请检查您设置是否正常链接到网络！")
            return
        }

        if (params[0] !is String)  return
        val errorInfo = params[0] as String
        val msg = "视频播放出错，失败原因: $errorInfo"
        showWarnStatus(msg)
    }

    override fun onVodComplete() {
        // 显示播放完成，重播ui
        showCompleteStatus()
    }


    override fun onVodRelease() {
        hide()
    }

    override fun onVodLockedScreen(vararg params: Any) {
    }

    override fun onVodFullScreen(vararg params: Any) {
    }

    override fun onVodLoadingBegin() {

    }

    override fun onVodLoadingProgress(vararg params: Any) {
    }

    override fun onVodLoadingEnd() {

    }

    override fun onVodBackPress(): Boolean {
        return false
    }

    override fun onEpisodeSelected(){
    }

    override fun onVodShowUnlockUI(isDisplayUI: Boolean) {
        if (isDisplayUI) {
            hide()
        }
    }

    override fun onVodSkits() {

    }
}